<?php
namespace PsiFramework\Classes\Util\Parameters\DAO;

import("Classes.Util.DAO.IPrimitiveObjectDAO");

use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;
use \PsiFramework\Classes\Util\Manager\ObjectManager;
use \PsiFramework\Classes\Util\DAO\IPrimitiveObjectDAO;
use \PsiFramework\Classes\Util\Object\ReferenceObject;
use \PsiFramework\Classes\Util\UI\HTML;
use \PsiFramework\Classes\Util\UI\Printer;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 *
 *
 * @package DAO
 */
class JsonParametersDAO implements IPrimitiveObjectDAO
{

    protected static $_singleton = null;

    protected function getParamLabelIdentifier(/*string*/ $paramName)
    {
        return $paramName . '_name' ;
    }

    protected function getParamIdIdentifier(/*string*/ $paramName)
    {
        return $paramName . '_id' ;
    }

    protected function getParamTypeIdentifier(/*string*/ $paramName)
    {
        return $paramName . '_type' ;
    }

    public function parse($value=null, $options=null)
    {

    }

    public function edit(
        IPrimitiveObject $parameters=null,
        $name=null,
        $options=null
    )
    {

        $html = '' ;

        if (!is_null($parameters)) {
            foreach ($parameters->getParameters() as $paramName=>$paramValue) {

                $html .= '<div class="label">'."\n" ;
                $html .= '    <span class="titre">'.
                                Printer::getText($paramName).
                             '</span>' . "\n" ;

                $value = $paramValue ;
                if ($value instanceof ReferenceObject) {

                    $type = $value->getType();
                    $value = $value->getObject();

                    if ($value instanceof Object) {

                        $paramLabel
                            = $this->getParamLabelIdentifier($paramName);
                        $paramId = $this->getParamIdIdentifier($paramName);
                        $paramType = $this->getParamTypeIdentifier($paramName);

                        $html .= '    <input '.
                                        'class="firstInput" '.
                                        'type="text" '.
                                        'id="'.Printer::getText(
                                            $paramLabel
                                        ).'" '.
                                        'name="'.Printer::getText(
                                            $paramLabel
                                        ).'" '.
                                        'value="'.Printer::getText(
                                            $value->toString()
                                        ).'" '.
                                      '/>' . "\n" ;
                        $html .= '    <input '.
                                'class="firstInput" '.
                                'type="text" '.
                                'id="'.Printer::getText(
                                    $paramType
                                ).'" '.
                                'name="'.Printer::getText(
                                    $paramType
                                ).'" '.
                                'value="'.Printer::getText(
                                    $type
                                ).'" '.
                                '/>' . "\n" ;
                        $html .= '    <input '.
                                'class="firstInput" '.
                                'type="text" '.
                                'id="'.Printer::getText(
                                    $paramId
                                ).'" '.
                                'name="'.Printer::getText(
                                    $paramId
                                ).'" '.
                                'value="'.Printer::getText(
                                    $value->getId()
                                ).'" '.
                                '/>' . "\n" ;
                        
                        $html .= '<script type="text/javascript">'."\n".
                                 '    $('.
                                         '"#'.
                                         Printer::getText($paramLabel).
                                         '"'.
                                     ').autocomplete({'."\n".
                                 '        source: '.
                                             '"?service=ListPages&'.
                                             'format=json",'."\n".
                                 '        select: function(event, ui) {'."\n".
                                 '            $('.
                                                  '"#'.
                                                  Printer::getText($paramLabel).
                                                  '"'.
                                             ').val(ui.item.label);'."\n".
                                 '        $('.
                                             '"#'.
                                             Printer::getText($paramId).
                                             '"'.
                                         ').val(ui.item.id);'."\n".
                                 '        }'."\n".
                                 '    });'."\n".
                                 '</script>'."\n" ;
                    } else if ($value instanceof IPrimitiveObject) {
                        $dao = ObjectManager::getInstance()->
                                   getPrimitiveObjectDaoInstance($type);
                        $html .= $dao->edit($value);
                    }
                } else {
                    $html .= '    <input '.
                                    'class="firstInput" '.
                                    'type="text" '.
                                    'id="'.Printer::getText(
                                        $paramName
                                    ).'" '.
                                    'name="'.Printer::getText(
                                        $paramName
                                    ).'" '.
                                    'value="'.Printer::getText(
                                        $value
                                    ).'" '.
                                    '/>' . "\n" ;
                }

                $html .= '</div>' . "\n" ;
            }
        }

        return $html ;
    }

    public function display(IPrimitiveObject $object=null, $options=null)
    {

    }

    public function javascript(IPrimitiveObject $object=null, $options=null)
    {
        if (!is_null($parameters)) {
            foreach ($parameters->getParameters() as $paramName=>$paramValue) {

                $value = $paramValue ;
                if ($value instanceof ReferenceObject) {

                    $type = $value->getType();
                    $value = $value->getObject();

                    if ($value instanceof Object) {
                        $paramLabel = $this->
                            getParamLabelIdentifier($paramName);
                        $paramId = $this->getParamIdIdentifier($paramName);

                        HTML::addAutoComplete($paramLabel, $paramId);
                    } else if ($value instanceof IPrimitiveObject) {
                        $dao = ObjectManager::getInstance()->
                            getPrimitiveObjectDaoInstance($type);
                        $html .= $dao->javascript($value);
                    }
                }
            }
        }
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return     JsonParametersDAO instance
     */
    public static function getInstance()
    {

        if (self::$_singleton==null) {
            self::$_singleton = new JsonParametersDAO();
        }

        return self::$_singleton;
    }
}
